package com.bawei.day14_service;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

public class Main3Activity extends AppCompatActivity {
    MyService3.MyBinder myBinder;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService3.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        findViewById(R.id.bt_bind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              start_bind();
            }
        });
        findViewById(R.id.bt_unbind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop_unbind();

            }
        });
        //调用服务里面的方法
        findViewById(R.id.bt_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myBinder.next();

            }
        });
    }
    //解除绑定+停止
    private void stop_unbind() {
        Intent intent = new Intent(this,MyService3.class);
        unbindService(serviceConnection);
        stopService(intent);
    }
    //启动+绑定
    private void start_bind() {
        Intent intent = new Intent(this,MyService3.class);
        startService(intent);
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);

    }
}
